using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

//Game Genie Encoder/Decoder for NES
//Copyright 2008 Shawn M. Crawford

namespace gamegenie_decoder_encoder
{
    public partial class nesGGEncDecForm : Form
    {
        string codeGenericBinaryString, binary0String, binary1String, binary2String,
            binary3String, binary4String, binary5String, binary6String, binary7String,
            binary8String, binary9String, binary10String, binary11String, binary12String,
            binary13String, binary14String, binary15String, binary16String, binary17String,
            binary18String, binary19String, binary20String, binary21String, binary22String,
            binary23String, binary24String, binary25String, binary26String, binary27String,
            binary28String, binary29String, binary30String, binary31String, allBinaryString,
            allBinaryString2, firstFourString, secondFourString, thirdFourString, fourthFourString,
            fifthFourString, sixthFourString, seventhFourString, eighthFourString, tempFourString,
            firECString, secECString, thiECString, fouECString, fifECString, sixECString,
            sevECString, eigECString, tempECString, highBitString, regBit1String, regBit2String,
            regBit3String;

        int counterBinaryLengthInt;

        //this is for the keydown/keytext methods
        private bool validInput = false;

        public nesGGEncDecForm()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string DoBinaryFix()
        {
            try
            {
                if (codeGenericBinaryString.Length == 3)
                {
                    codeGenericBinaryString = "0" + codeGenericBinaryString;
                }
                if (codeGenericBinaryString.Length == 2)
                {
                    codeGenericBinaryString = "00" + codeGenericBinaryString;
                }
                if (codeGenericBinaryString.Length == 1)
                {
                    codeGenericBinaryString = "000" + codeGenericBinaryString;
                }
            }
            catch
            {
                MessageBox.Show("I crashed doing Binary Fix.", "Error 1:", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            return codeGenericBinaryString;
        }

        private string ChangeBinaryToHex()
        {
            string tempString = "";
            //convert to decimal
            //tempString = Convert.ToString(Convert.ToInt32(tempFourString, 2));
            //tempString = tempFourString;
            //test1TextBox.Text = tempFourString;
            //decimal to hex
            try
            {
                if (tempFourString == "0000")
                {
                    tempString = "0";
                }
                if (tempFourString == "0001")
                {
                    tempString = "1";
                }
                if (tempFourString == "0010")
                {
                    tempString = "2";
                }
                if (tempFourString == "0011")
                {
                    tempString = "3";
                }
                if (tempFourString == "0100")
                {
                    tempString = "4";
                }
                if (tempFourString == "0101")
                {
                    tempString = "5";
                }
                if (tempFourString == "0110")
                {
                    tempString = "6";
                }
                if (tempFourString == "0111")
                {
                    tempString = "7";
                }
                if (tempFourString == "1000")
                {
                    tempString = "8";
                }
                if (tempFourString == "1001")
                {
                    tempString = "9";
                }
                if (tempFourString == "1010")
                {
                    tempString = "a";
                }
                if (tempFourString == "1011")
                {
                    tempString = "b";
                }
                if (tempFourString == "1100")
                {
                    tempString = "c";
                }
                if (tempFourString == "1101")
                {
                    tempString = "d";
                }
                if (tempFourString == "1110")
                {
                    tempString = "e";
                }
                if (tempFourString == "1111")
                {
                    tempString = "f";
                }
            }
            catch
            {
                MessageBox.Show("I crashed doing Binary to Hex.", "Error 2:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return tempString;
        }

        private string CallArrayForBitRotation()
        {
            char[] bc = allBinaryString.ToCharArray();

            counterBinaryLengthInt = bc.Length;

            try
            {

                if (counterBinaryLengthInt == 32)
                {

                    binary31String = bc[31].ToString();
                    binary30String = bc[30].ToString();
                    binary29String = bc[29].ToString();
                    binary28String = bc[28].ToString();
                    binary27String = bc[27].ToString();
                    binary26String = bc[26].ToString();
                    binary25String = bc[25].ToString();
                    binary24String = bc[24].ToString();
                    binary23String = bc[23].ToString();
                    binary22String = bc[22].ToString();
                    binary21String = bc[21].ToString();
                    binary20String = bc[20].ToString();
                    binary19String = bc[19].ToString();
                    binary18String = bc[18].ToString();
                    binary17String = bc[17].ToString();
                    binary16String = bc[16].ToString();
                    binary15String = bc[15].ToString();
                    binary14String = bc[14].ToString();
                    binary13String = bc[13].ToString();
                    binary12String = bc[12].ToString();
                    binary11String = bc[11].ToString();
                    binary10String = bc[10].ToString();
                    binary9String = bc[9].ToString();
                    binary8String = bc[8].ToString();
                    binary7String = bc[7].ToString();
                    binary6String = bc[6].ToString();
                    binary5String = bc[5].ToString();
                    binary4String = bc[4].ToString();
                    binary3String = bc[3].ToString();
                    binary2String = bc[2].ToString();
                    binary1String = bc[1].ToString();
                    binary0String = bc[0].ToString();


                    //now we rotate the bit (this is for the 32 binary code)
                    allBinaryString = binary31String + binary0String + binary1String + binary2String
                    + binary3String + binary4String
                    + binary5String + binary6String + binary7String + binary8String + binary9String
                    + binary10String + binary11String + binary12String + binary13String + binary14String
                    + binary15String + binary16String + binary17String + binary18String + binary19String
                    + binary20String + binary21String + binary22String + binary23String + binary24String
                    + binary25String + binary26String + binary27String + binary28String + binary29String
                    + "\n" + binary30String;
                    //for some reason the bit rotation breaks without the newline
                    //so we add it then remove it,
                    allBinaryString2 = allBinaryString.Replace("\n", "");
                }

                if (counterBinaryLengthInt == 24)
                {
                    binary23String = bc[23].ToString();
                    binary22String = bc[22].ToString();
                    binary21String = bc[21].ToString();
                    binary20String = bc[20].ToString();
                    binary19String = bc[19].ToString();
                    binary18String = bc[18].ToString();
                    binary17String = bc[17].ToString();
                    binary16String = bc[16].ToString();
                    binary15String = bc[15].ToString();
                    binary14String = bc[14].ToString();
                    binary13String = bc[13].ToString();
                    binary12String = bc[12].ToString();
                    binary11String = bc[11].ToString();
                    binary10String = bc[10].ToString();
                    binary9String = bc[9].ToString();
                    binary8String = bc[8].ToString();
                    binary7String = bc[7].ToString();
                    binary6String = bc[6].ToString();
                    binary5String = bc[5].ToString();
                    binary4String = bc[4].ToString();
                    binary3String = bc[3].ToString();
                    binary2String = bc[2].ToString();
                    binary1String = bc[1].ToString();
                    binary0String = bc[0].ToString();

                    //now we rotate the bit (this is for the 24 binary code)
                    allBinaryString = binary23String + binary0String + binary1String
                    + binary2String + binary3String + binary4String + binary5String
                    + binary6String + binary7String + binary8String + binary9String
                    + binary10String + binary11String + binary12String + binary13String
                    + binary14String + binary15String + binary16String + binary17String
                    + binary18String + binary19String + binary20String + binary21String
                    + "\n" + binary22String;
                    //for some reason the bit rotation breaks without the newline
                    //so we add it then remove it,
                    allBinaryString2 = allBinaryString.Replace("\n", "");

                }
                return allBinaryString2;
            }
            catch
            {
                MessageBox.Show("I crashed doing a binary string to an array.", "Error 3:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return allBinaryString2;
        }
        private void gameGenieCode2TextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
        // Initialize the flag to false.
        //we set the keypress and keydown in the event panel
        validInput  = false;
        //Only allow AEPOZXLUGKISTVYN & backspace
        try
        {
            if (
                    e.KeyCode == Keys.A || e.KeyCode == Keys.E ||
                    e.KeyCode == Keys.P || e.KeyCode == Keys.O ||
                    e.KeyCode == Keys.Z || e.KeyCode == Keys.X ||
                    e.KeyCode == Keys.L || e.KeyCode == Keys.U ||
                    e.KeyCode == Keys.G || e.KeyCode == Keys.K ||
                    e.KeyCode == Keys.I || e.KeyCode == Keys.S ||
                    e.KeyCode == Keys.T || e.KeyCode == Keys.V ||
                    e.KeyCode == Keys.Y || e.KeyCode == Keys.N ||
                    e.KeyCode == Keys.Back
                //uncommenting this will allow control-V (paste)
                //|| e.KeyCode == (Keys.Control | Keys.V) ||
                //e.KeyCode == (Keys.Shift | Keys.Insert)
               )
            {
                validInput = false;
            }
            else
            {
                validInput = true;
            }
        }
        catch
        {
            MessageBox.Show("I crashed blocking your keys.", "Error 4:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        }
        
        private void gameGenieCode2TextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (validInput == true)
            {
            // Stop the character from being entered
            e.Handled = true;
            }
        }

        private void decipherButton_Click(object sender, EventArgs e)
        {
            string gameGenieCodeDecipherString, aString, bString, cString, dString,
                    eString, fString, gString, hString,
                    codeOneBinaryString, codeTwoBinaryString, codeThreeBinaryString, 
                    codeFourBinaryString, codeFiveBinaryString, codeSixBinaryString,
                    codeSevenBinaryString, codeEightBinaryString;
            
            int counterInt, errorCounterInt, codeOneInt, codeTwoInt, codeThreeInt,
                codeFourInt, codeFiveInt, codeSixInt, codeSevenInt, codeEightInt,
                binaryBaseInt;

            binaryBaseInt = 2;
            errorCounterInt = 0;
            allBinaryString = "0";

            aString = "w";
            bString = "w";
            cString = "w";
            dString = "w";
            eString = "w";
            fString = "w";
            gString = "w";
            hString = "w";
            codeOneInt = 0;
            codeTwoInt = 0;
            codeThreeInt = 0;
            codeFourInt = 0;
            codeFiveInt = 0;
            codeSixInt = 0;
            codeSevenInt = 0;
            codeEightInt = 0;
            codeOneBinaryString = "";
            codeTwoBinaryString = "";
            codeThreeBinaryString = "";
            codeFourBinaryString = "";
            codeFiveBinaryString = "";
            codeSixBinaryString = "";
            codeSevenBinaryString = "";
            codeEightBinaryString = "";

            gameGenieCodeDecipherString = gameGenieCode2TextBox.Text;

            StringBuilder sbBuild = new StringBuilder();
            char[] ggca = gameGenieCodeDecipherString.ToCharArray();

            counterInt = ggca.Length;

            try
            {
                //for wrong length of code entered
                if (counterInt <= 5 || counterInt == 7 || counterInt >= 9)
                {
                    MessageBox.Show("Codes must be 6 or 8 characters.", "Code Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    errorCounterInt = 1;
                }

                if (errorCounterInt == 0)
                {
                    if (counterInt == 8)
                    {
                        hString = ggca[7].ToString();
                        gString = ggca[6].ToString();
                    }
                    if (counterInt == 8 || counterInt == 6)
                    {
                        fString = ggca[5].ToString();
                        eString = ggca[4].ToString();
                        dString = ggca[3].ToString();
                        cString = ggca[2].ToString();
                        bString = ggca[1].ToString();
                        aString = ggca[0].ToString();
                    }
                }
            }
            catch
            {
                MessageBox.Show("I crashed creating an array from your game genie code.", "Error 5:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                if (aString == "A" || aString == "a")
                {
                    codeOneInt = 0;
                }
                if (bString == "A" || bString == "a")
                {
                    codeTwoInt = 0;
                }
                if (cString == "A" || cString == "a")
                {
                    codeThreeInt = 0;
                }
                if (dString == "A" || dString == "a")
                {
                    codeFourInt = 0;
                }
                if (eString == "A" || eString == "a")
                {
                    codeFiveInt = 0;
                }
                if (fString == "A" || fString == "a")
                {
                    codeSixInt = 0;
                }

                if (counterInt == 8)
                {
                    if (gString == "A" || gString == "a")
                    {
                        codeSevenInt = 0;
                    }
                    if (hString == "A" || hString == "a")
                    {
                        codeEightInt = 0;
                    }
                }

                if (aString == "E" || aString == "e")
                {
                    codeOneInt = 1;
                }
                if (bString == "E" || bString == "e")
                {
                    codeTwoInt = 1;
                }
                if (cString == "E" || cString == "e")
                {
                    codeThreeInt = 1;
                }
                if (dString == "E" || dString == "e")
                {
                    codeFourInt = 1;
                }
                if (eString == "E" || eString == "e")
                {
                    codeFiveInt = 1;
                }
                if (fString == "E" || fString == "e")
                {
                    codeSixInt = 1;
                }

                if (counterInt == 8)
                {
                    if (gString == "E" || gString == "e")
                    {
                        codeSevenInt = 1;
                    }
                    if (hString == "E" || hString == "e")
                    {
                        codeEightInt = 1;
                    }
                }
                if (aString == "P" || aString == "p")
                {
                    codeOneInt = 2;
                }
                if (bString == "P" || bString == "p")
                {
                    codeTwoInt = 2;
                }
                if (cString == "P" || cString == "p")
                {
                    codeThreeInt = 2;
                }
                if (dString == "P" || dString == "p")
                {
                    codeFourInt = 2;
                }
                if (eString == "P" || eString == "p")
                {
                    codeFiveInt = 2;
                }
                if (fString == "P" || fString == "p")
                {
                    codeSixInt = 2;
                }
                if (counterInt == 8)
                {
                    if (gString == "P" || gString == "p")
                    {
                        codeSevenInt = 2;
                    }
                    if (hString == "P" || hString == "p")
                    {
                        codeEightInt = 2;
                    }
                }
                if (aString == "O" || aString == "o")
                {
                    codeOneInt = 3;
                }
                if (bString == "O" || bString == "o")
                {
                    codeTwoInt = 3;
                }
                if (cString == "O" || cString == "o")
                {
                    codeThreeInt = 3;
                }
                if (dString == "O" || dString == "o")
                {
                    codeFourInt = 3;
                }
                if (eString == "O" || eString == "o")
                {
                    codeFiveInt = 3;
                }
                if (fString == "O" || fString == "o")
                {
                    codeSixInt = 3;
                }
                if (counterInt == 8)
                {
                    if (gString == "O" || gString == "o")
                    {
                        codeSevenInt = 3;
                    }
                    if (hString == "O" || hString == "o")
                    {
                        codeEightInt = 3;
                    }
                }
                if (aString == "Z" || aString == "z")
                {
                    codeOneInt = 4;
                }
                if (bString == "Z" || bString == "z")
                {
                    codeTwoInt = 4;
                }
                if (cString == "Z" || cString == "z")
                {
                    codeThreeInt = 4;
                }
                if (dString == "Z" || dString == "z")
                {
                    codeFourInt = 4;
                }
                if (eString == "Z" || eString == "z")
                {
                    codeFiveInt = 4;
                }
                if (fString == "Z" || fString == "z")
                {
                    codeSixInt = 4;
                }
                if (counterInt == 8)
                {
                    if (gString == "Z" || gString == "z")
                    {
                        codeSevenInt = 4;
                    }
                    if (hString == "Z" || hString == "z")
                    {
                        codeEightInt = 4;
                    }
                }
                if (aString == "X" || aString == "x")
                {
                    codeOneInt = 5;
                }
                if (bString == "X" || bString == "x")
                {
                    codeTwoInt = 5;
                }
                if (cString == "X" || cString == "x")
                {
                    codeThreeInt = 5;
                }
                if (dString == "X" || dString == "x")
                {
                    codeFourInt = 5;
                }
                if (eString == "X" || eString == "x")
                {
                    codeFiveInt = 5;
                }
                if (fString == "X" || fString == "x")
                {
                    codeSixInt = 5;
                }
                if (counterInt == 8)
                {
                    if (gString == "X" || gString == "x")
                    {
                        codeSevenInt = 5;
                    }
                    if (hString == "X" || hString == "x")
                    {
                        codeEightInt = 5;
                    }
                }
                if (aString == "L" || aString == "l")
                {
                    codeOneInt = 6;
                }
                if (bString == "L" || bString == "l")
                {
                    codeTwoInt = 6;
                }
                if (cString == "L" || cString == "l")
                {
                    codeThreeInt = 6;
                }
                if (dString == "L" || dString == "l")
                {
                    codeFourInt = 6;
                }
                if (eString == "L" || eString == "l")
                {
                    codeFiveInt = 6;
                }
                if (fString == "L" || fString == "l")
                {
                    codeSixInt = 6;
                }
                if (counterInt == 8)
                {
                    if (gString == "L" || gString == "l")
                    {
                        codeSevenInt = 6;
                    }
                    if (hString == "L" || hString == "l")
                    {
                        codeEightInt = 6;
                    }
                }
                if (aString == "U" || aString == "u")
                {
                    codeOneInt = 7;
                }
                if (bString == "U" || bString == "u")
                {
                    codeTwoInt = 7;
                }
                if (cString == "U" || cString == "u")
                {
                    codeThreeInt = 7;
                }
                if (dString == "U" || dString == "u")
                {
                    codeFourInt = 7;
                }
                if (eString == "U" || eString == "u")
                {
                    codeFiveInt = 7;
                }
                if (fString == "U" || fString == "u")
                {
                    codeSixInt = 7;
                }
                if (counterInt == 8)
                {
                    if (gString == "U" || gString == "u")
                    {
                        codeSevenInt = 7;
                    }
                    if (hString == "U" || hString == "u")
                    {
                        codeEightInt = 7;
                    }
                }
                if (aString == "G" || aString == "g")
                {
                    codeOneInt = 8;
                }
                if (bString == "G" || bString == "g")
                {
                    codeTwoInt = 8;
                }
                if (cString == "G" || cString == "g")
                {
                    codeThreeInt = 8;
                }
                if (dString == "G" || dString == "g")
                {
                    codeFourInt = 8;
                }
                if (eString == "G" || eString == "g")
                {
                    codeFiveInt = 8;
                }
                if (fString == "G" || fString == "g")
                {
                    codeSixInt = 8;
                }
                if (counterInt == 8)
                {
                    if (gString == "G" || gString == "g")
                    {
                        codeSevenInt = 8;
                    }
                    if (hString == "G" || hString == "g")
                    {
                        codeEightInt = 8;
                    }
                }
                if (aString == "K" || aString == "k")
                {
                    codeOneInt = 9;
                }
                if (bString == "K" || bString == "k")
                {
                    codeTwoInt = 9;
                }
                if (cString == "K" || cString == "k")
                {
                    codeThreeInt = 9;
                }
                if (dString == "K" || dString == "k")
                {
                    codeFourInt = 9;
                }
                if (eString == "K" || eString == "k")
                {
                    codeFiveInt = 9;
                }
                if (fString == "K" || fString == "k")
                {
                    codeSixInt = 9;
                }
                if (counterInt == 8)
                {
                    if (gString == "K" || gString == "k")
                    {
                        codeSevenInt = 9;
                    }
                    if (hString == "K" || hString == "k")
                    {
                        codeEightInt = 9;
                    }
                }
                if (aString == "I" || aString == "i")
                {
                    codeOneInt = 10;
                }
                if (bString == "I" || bString == "i")
                {
                    codeTwoInt = 10;
                }
                if (cString == "I" || cString == "i")
                {
                    codeThreeInt = 10;
                }
                if (dString == "I" || dString == "i")
                {
                    codeFourInt = 10;
                }
                if (eString == "I" || eString == "i")
                {
                    codeFiveInt = 10;
                }
                if (fString == "I" || fString == "i")
                {
                    codeSixInt = 10;
                }
                if (counterInt == 8)
                {
                    if (gString == "I" || gString == "i")
                    {
                        codeSevenInt = 10;
                    }
                    if (hString == "I" || hString == "i")
                    {
                        codeEightInt = 10;
                    }
                }
                if (aString == "S" || aString == "s")
                {
                    codeOneInt = 11;
                }
                if (bString == "S" || bString == "s")
                {
                    codeTwoInt = 11;
                }
                if (cString == "S" || cString == "s")
                {
                    codeThreeInt = 11;
                }
                if (dString == "S" || dString == "s")
                {
                    codeFourInt = 11;
                }
                if (eString == "S" || eString == "s")
                {
                    codeFiveInt = 11;
                }
                if (fString == "S" || fString == "s")
                {
                    codeSixInt = 11;
                }
                if (counterInt == 8)
                {
                    if (gString == "S" || gString == "s")
                    {
                        codeSevenInt = 11;
                    }
                    if (hString == "S" || hString == "s")
                    {
                        codeEightInt = 11;
                    }
                }
                if (aString == "T" || aString == "t")
                {
                    codeOneInt = 12;
                }
                if (bString == "T" || bString == "t")
                {
                    codeTwoInt = 12;
                }
                if (cString == "T" || cString == "t")
                {
                    codeThreeInt = 12;
                }
                if (dString == "T" || dString == "t")
                {
                    codeFourInt = 12;
                }
                if (eString == "T" || eString == "t")
                {
                    codeFiveInt = 12;
                }
                if (fString == "T" || fString == "t")
                {
                    codeSixInt = 12;
                }
                if (counterInt == 8)
                {
                    if (gString == "T" || gString == "t")
                    {
                        codeSevenInt = 12;
                    }
                    if (hString == "T" || hString == "t")
                    {
                        codeEightInt = 12;
                    }
                }
                if (aString == "V" || aString == "v")
                {
                    codeOneInt = 13;
                }
                if (bString == "V" || bString == "v")
                {
                    codeTwoInt = 13;
                }
                if (cString == "V" || cString == "v")
                {
                    codeThreeInt = 13;
                }
                if (dString == "V" || dString == "v")
                {
                    codeFourInt = 13;
                }
                if (eString == "V" || eString == "v")
                {
                    codeFiveInt = 13;
                }
                if (fString == "V" || fString == "v")
                {
                    codeSixInt = 13;
                }
                if (counterInt == 8)
                {
                    if (gString == "V" || gString == "v")
                    {
                        codeSevenInt = 13;
                    }
                    if (hString == "V" || hString == "v")
                    {
                        codeEightInt = 13;
                    }
                }
                if (aString == "Y" || aString == "y")
                {
                    codeOneInt = 14;
                }
                if (bString == "Y" || bString == "y")
                {
                    codeTwoInt = 14;
                }
                if (cString == "Y" || cString == "y")
                {
                    codeThreeInt = 14;
                }
                if (dString == "Y" || dString == "y")
                {
                    codeFourInt = 14;
                }
                if (eString == "Y" || eString == "y")
                {
                    codeFiveInt = 14;
                }
                if (fString == "Y" || fString == "y")
                {
                    codeSixInt = 14;
                }
                if (counterInt == 8)
                {
                    if (gString == "Y" || gString == "y")
                    {
                        codeSevenInt = 14;
                    }
                    if (hString == "Y" || hString == "y")
                    {
                        codeEightInt = 14;
                    }
                }
                if (aString == "N" || aString == "n")
                {
                    codeOneInt = 15;
                }
                if (bString == "N" || bString == "n")
                {
                    codeTwoInt = 15;
                }
                if (cString == "N" || cString == "n")
                {
                    codeThreeInt = 15;
                }
                if (dString == "N" || dString == "n")
                {
                    codeFourInt = 15;
                }
                if (eString == "N" || eString == "n")
                {
                    codeFiveInt = 15;
                }
                if (fString == "N" || fString == "n")
                {
                    codeSixInt = 15;
                }
                if (counterInt == 8)
                {
                    if (gString == "N" || gString == "n")
                    {
                        codeSevenInt = 15;
                    }
                    if (hString == "N" || hString == "n")
                    {
                        codeEightInt = 15;
                    }
                }
            }
            catch
            {
                MessageBox.Show("I crashed converting your genie code to integers.", "Error 6:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            try
            {
                codeOneBinaryString = Convert.ToString(codeOneInt, binaryBaseInt);
                codeTwoBinaryString = Convert.ToString(codeTwoInt, binaryBaseInt);
                codeThreeBinaryString = Convert.ToString(codeThreeInt, binaryBaseInt);
                codeFourBinaryString = Convert.ToString(codeFourInt, binaryBaseInt);
                codeFiveBinaryString = Convert.ToString(codeFiveInt, binaryBaseInt);
                codeSixBinaryString = Convert.ToString(codeSixInt, binaryBaseInt);

                if (counterInt == 8)
                {
                    codeSevenBinaryString = Convert.ToString(codeSevenInt, binaryBaseInt);
                    codeEightBinaryString = Convert.ToString(codeEightInt, binaryBaseInt);
                }
            }
            catch
            {
                MessageBox.Show("I crashed converting you genie ints to binary string.", "Error 7:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            codeGenericBinaryString = codeOneBinaryString;
            DoBinaryFix();
            codeOneBinaryString = DoBinaryFix();

            codeGenericBinaryString = codeTwoBinaryString;
            DoBinaryFix();
            codeTwoBinaryString = DoBinaryFix();

            codeGenericBinaryString = codeThreeBinaryString;
            DoBinaryFix();
            codeThreeBinaryString = DoBinaryFix();

            codeGenericBinaryString = codeFourBinaryString;
            DoBinaryFix();
            codeFourBinaryString = DoBinaryFix();

            codeGenericBinaryString = codeFiveBinaryString;
            DoBinaryFix();
            codeFiveBinaryString = DoBinaryFix();

            codeGenericBinaryString = codeSixBinaryString;
            DoBinaryFix();
            codeSixBinaryString = DoBinaryFix();

            if (counterInt == 8)
            {
                codeGenericBinaryString = codeSevenBinaryString;
                DoBinaryFix();
                codeSevenBinaryString = DoBinaryFix();

                codeGenericBinaryString = codeEightBinaryString;
                DoBinaryFix();
                codeEightBinaryString = DoBinaryFix();
            }

            try
            {
                if (counterInt == 8)
                {
                    allBinaryString = codeOneBinaryString + codeTwoBinaryString + codeThreeBinaryString +
                        codeFourBinaryString + codeFiveBinaryString + codeSixBinaryString +
                        codeSevenBinaryString + codeEightBinaryString;

                    CallArrayForBitRotation();
                    allBinaryString = CallArrayForBitRotation();
                }
            }
            catch
            {
                MessageBox.Show("I crashed calling the bit rotation.", "Error 8:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            try
            {
                if (counterInt == 6)
                {
                    allBinaryString = codeOneBinaryString + codeTwoBinaryString + codeThreeBinaryString +
                        codeFourBinaryString + codeFiveBinaryString + codeSixBinaryString;

                    CallArrayForBitRotation();
                    allBinaryString = CallArrayForBitRotation();
                }
            }
            catch
            {
                MessageBox.Show("I crashed calling the bit rotation.", "Error 9:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            /* We can load the binary into an array and re-order it
             * that way. We need to accomplish the following:
             * 0011-0101-1111-1000-0110-0100-1101-1001
             * Rotating one bit rightward gives:
             * 1001-1010-1111-1100-0011-0010-0110-1100
             */
                if (counterInt == 8 || counterInt == 6)
                {
                    //create another array
                    char[] firstFourChar = new char[32];
                    StringReader stringreader = new StringReader(allBinaryString);

                    stringreader.Read(firstFourChar, 0, 32);

                    try
                    {
                        //output first four
                        firstFourString = (firstFourChar[0]).ToString();
                        firstFourString += (firstFourChar[1]).ToString();
                        firstFourString += (firstFourChar[2]).ToString();
                        firstFourString += (firstFourChar[3]).ToString();

                        //send to method to convert to hex
                        tempFourString = firstFourString;
                        ChangeBinaryToHex();
                        firstFourString = ChangeBinaryToHex();

                        //next four
                        secondFourString = (firstFourChar[4]).ToString();
                        secondFourString += (firstFourChar[5]).ToString();
                        secondFourString += (firstFourChar[6]).ToString();
                        secondFourString += (firstFourChar[7]).ToString();

                        tempFourString = secondFourString;
                        ChangeBinaryToHex();
                        secondFourString = ChangeBinaryToHex();
                        //firstFourTextBox.Text += secondFourString;

                        //and so on
                        thirdFourString = (firstFourChar[8]).ToString();
                        thirdFourString += (firstFourChar[9]).ToString();
                        thirdFourString += (firstFourChar[10]).ToString();
                        thirdFourString += (firstFourChar[11]).ToString();

                        tempFourString = thirdFourString;
                        ChangeBinaryToHex();
                        thirdFourString = ChangeBinaryToHex();
                        //firstFourTextBox.Text += thirdFourString;

                        fourthFourString = (firstFourChar[12]).ToString();
                        //drop the high bit
                        if (fourthFourString == "1")
                        {
                            fourthFourString = "0";
                        }

                        fourthFourString += (firstFourChar[13]).ToString();
                        fourthFourString += (firstFourChar[14]).ToString();
                        fourthFourString += (firstFourChar[15]).ToString();

                        tempFourString = fourthFourString;
                        ChangeBinaryToHex();
                        fourthFourString = ChangeBinaryToHex();

                        fifthFourString = (firstFourChar[16]).ToString();
                        fifthFourString += (firstFourChar[17]).ToString();
                        fifthFourString += (firstFourChar[18]).ToString();
                        fifthFourString += (firstFourChar[19]).ToString();

                        tempFourString = fifthFourString;
                        ChangeBinaryToHex();
                        fifthFourString = ChangeBinaryToHex();

                        sixthFourString = (firstFourChar[20]).ToString();
                        sixthFourString += (firstFourChar[21]).ToString();
                        sixthFourString += (firstFourChar[22]).ToString();
                        sixthFourString += (firstFourChar[23]).ToString();

                        tempFourString = sixthFourString;
                        ChangeBinaryToHex();
                        sixthFourString = ChangeBinaryToHex();

                        if (counterInt == 8)
                        {
                            seventhFourString = (firstFourChar[24]).ToString();
                            seventhFourString += (firstFourChar[25]).ToString();
                            seventhFourString += (firstFourChar[26]).ToString();
                            seventhFourString += (firstFourChar[27]).ToString();

                            tempFourString = seventhFourString;
                            ChangeBinaryToHex();
                            seventhFourString = ChangeBinaryToHex();

                            eighthFourString = (firstFourChar[28]).ToString();
                            eighthFourString += (firstFourChar[29]).ToString();
                            eighthFourString += (firstFourChar[30]).ToString();
                            eighthFourString += (firstFourChar[31]).ToString();

                            tempFourString = eighthFourString;
                            ChangeBinaryToHex();
                            eighthFourString = ChangeBinaryToHex();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("I crashed with array problems.", "Error 10:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    stringreader.Close();
            
        

             
                //now put the values in the correct place [FECADBHG] -> [ABCDEFGH]
                address2TextBox.Text = "$" + fourthFourString + sixthFourString + thirdFourString + fifthFourString;
                replaceValue2TextBox.Text = "$" + secondFourString + firstFourString;

                //clear if code is 6 chars
                compareValue2TextBox.Clear();

                if (counterInt == 8)
                {
                    compareValue2TextBox.Text = "$" + eighthFourString + seventhFourString;
                }
                }
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutbox = new AboutBox1();

            aboutbox.ShowDialog();
        }

        private void encipherToolStripMenuItem_Click(object sender, EventArgs e)
        {
            encipherButton_Click(sender, e);
        }

        private void decipherToolStripMenuItem_Click(object sender, EventArgs e)
        {
            decipherButton_Click(sender, e);
        }

        private void addressTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            //we set the keypress and keydown in the event panel
            validInput = false;

            try
            {
                //Only allow A through F and numbers, and backspace
                if (
                        e.KeyCode == Keys.A || e.KeyCode == Keys.B ||
                        e.KeyCode == Keys.C || e.KeyCode == Keys.D ||
                        e.KeyCode == Keys.E || e.KeyCode == Keys.F ||
                        e.KeyCode == Keys.D0 || e.KeyCode == Keys.D1 ||
                        e.KeyCode == Keys.D2 || e.KeyCode == Keys.D3 ||
                        e.KeyCode == Keys.D4 || e.KeyCode == Keys.D5 ||
                        e.KeyCode == Keys.D6 || e.KeyCode == Keys.D7 ||
                        e.KeyCode == Keys.D8 || e.KeyCode == Keys.D9 ||
                        e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.NumPad1 ||
                        e.KeyCode == Keys.NumPad2 || e.KeyCode == Keys.NumPad3 ||
                        e.KeyCode == Keys.NumPad4 || e.KeyCode == Keys.NumPad5 ||
                        e.KeyCode == Keys.NumPad6 || e.KeyCode == Keys.NumPad7 ||
                        e.KeyCode == Keys.NumPad8 || e.KeyCode == Keys.NumPad9 ||
                        e.KeyCode == Keys.Back
                   )
                {
                    validInput = false;
                }
                else
                {
                    validInput = true;
                }
            }
        
                    catch
            {
                MessageBox.Show("I crashed blocking your keys.", "Error 11:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
}
        private void addressTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (validInput == true)
            {
                // Stop the character from being entered
                e.Handled = true;
            }
        }
        private void encipherButton_Click(object sender, EventArgs e)
        {
            string addressEncipherString, replaceValueEncipherString, compareValueEncipherString,
                completeEncipherString, ec1String, ec2String, ec3String, ec4String, ec5String, ec6String, ec7String,
                ec8String, ec9String, ec10String, ec11String, ec12String, ec13String, ec14String,
                ec15String, ec16String, ec17String, ec18String, ec19String, ec20String,
                ec21String, ec22String, ec23String, ec24String, ec25String, ec26String, ec27String,
                ec28String, ec29String, ec30String, ec31String, ec32String, allECString;

            int lengthCounterInt;

            ec25String = "0";
            ec26String = "0";
            ec27String = "0";
            ec28String = "0";
            ec29String = "0";
            ec30String = "0";
            ec31String = "0";
            ec32String = "0";

            //get the values
                addressEncipherString = addressTextBox.Text;
                replaceValueEncipherString = replaceValueTextBox.Text;
                compareValueEncipherString = compareValueTextBox.Text;

                //MessageBox.Show("I crashed getting values.", "Error 12:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
            completeEncipherString = addressEncipherString + replaceValueEncipherString
            + compareValueEncipherString;

            lengthCounterInt = completeEncipherString.Length;

            if (lengthCounterInt < 6 || lengthCounterInt > 8 || lengthCounterInt == 7 )
            {
                MessageBox.Show("Address must have 4 chars and replace value must have 2 chars.", "Code Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            else
            {
                char[] encipherChar = new char[8];

                StringReader stringreadertwo = new StringReader(completeEncipherString);

                stringreadertwo.Read(encipherChar, 0, 8);


                firECString = encipherChar[0].ToString();
                tempECString = firECString;
                DecodeEncipherString();
                firECString = DecodeEncipherString();

                secECString = encipherChar[1].ToString();
                tempECString = secECString;
                DecodeEncipherString();
                secECString = DecodeEncipherString();

                thiECString = encipherChar[2].ToString();
                tempECString = thiECString;
                DecodeEncipherString();
                thiECString = DecodeEncipherString();

                fouECString = encipherChar[3].ToString();
                tempECString = fouECString;
                DecodeEncipherString();
                fouECString = DecodeEncipherString();

                fifECString = encipherChar[4].ToString();
                tempECString = fifECString;
                DecodeEncipherString();
                fifECString = DecodeEncipherString();

                sixECString = encipherChar[5].ToString();
                tempECString = sixECString;
                DecodeEncipherString();
                sixECString = DecodeEncipherString();

                if (lengthCounterInt == 8)
                {

                    sevECString = encipherChar[6].ToString();
                    tempECString = sevECString;
                    DecodeEncipherString();
                    sevECString = DecodeEncipherString();

                    eigECString = encipherChar[7].ToString();
                    tempECString = eigECString;
                    DecodeEncipherString();
                    eigECString = DecodeEncipherString();
                }

                stringreadertwo.Close();

                char[] dropHighBitChar = new char[4];
                StringReader stringreaderthree = new StringReader(firECString);

                stringreaderthree.Read(dropHighBitChar, 0, 4);

                highBitString = dropHighBitChar[0].ToString();
                regBit1String = dropHighBitChar[1].ToString();
                regBit2String = dropHighBitChar[2].ToString();
                regBit3String = dropHighBitChar[3].ToString();

                //drop the high bit
                if (highBitString == "0")
                {
                    highBitString = "1";
                }
                //update firECString
                firECString = highBitString + regBit1String + regBit2String + regBit3String;

                stringreaderthree.Close();

                allECString = sixECString + fifECString + thiECString + firECString +
                    fouECString + secECString;

                if (lengthCounterInt == 8)
                {
                    allECString += eigECString + sevECString;
                }

                //Now we need to rotate the bit left
                char[] allECChar = new char[33];
                StringReader stringreaderfour = new StringReader(allECString);

                stringreaderfour.Read(allECChar, 0, 32);

                ec1String = allECChar[0].ToString();
                ec2String = allECChar[1].ToString();
                ec3String = allECChar[2].ToString();
                ec4String = allECChar[3].ToString();
                ec5String = allECChar[4].ToString();
                ec6String = allECChar[5].ToString();
                ec7String = allECChar[6].ToString();
                ec8String = allECChar[7].ToString();
                ec9String = allECChar[8].ToString();
                ec10String = allECChar[9].ToString();
                ec11String = allECChar[10].ToString();
                ec12String = allECChar[11].ToString();
                ec13String = allECChar[12].ToString();
                ec14String = allECChar[13].ToString();
                ec15String = allECChar[14].ToString();
                ec16String = allECChar[15].ToString();
                ec17String = allECChar[16].ToString();
                ec18String = allECChar[17].ToString();
                ec19String = allECChar[18].ToString();
                ec20String = allECChar[19].ToString();
                ec21String = allECChar[20].ToString();
                ec22String = allECChar[21].ToString();
                ec23String = allECChar[22].ToString();
                ec24String = allECChar[23].ToString();
                if (lengthCounterInt == 8)
                {
                    ec25String = allECChar[24].ToString();
                    ec26String = allECChar[25].ToString();
                    ec27String = allECChar[26].ToString();
                    ec28String = allECChar[27].ToString();
                    ec29String = allECChar[28].ToString();
                    ec30String = allECChar[29].ToString();
                    ec31String = allECChar[30].ToString();
                    ec32String = allECChar[31].ToString();

                    allECString = ec2String + ec3String + ec4String + ec5String + ec6String +
                        ec7String + ec8String + ec9String + ec10String + ec11String + ec12String + ec13String +
                    ec14String + ec15String + ec16String + ec17String + ec18String + ec19String + ec20String +
                        ec21String + ec22String + ec23String + ec24String + ec25String + ec26String + ec27String +
                    ec28String + ec29String + ec30String + ec31String + ec32String + ec1String;
                }

                if (lengthCounterInt == 6)
                {
                    allECString = ec2String + ec3String + ec4String + ec5String + ec6String +
                    ec7String + ec8String + ec9String + ec10String + ec11String + ec12String + ec13String +
                    ec14String + ec15String + ec16String + ec17String + ec18String + ec19String + ec20String +
                    ec21String + ec22String + ec23String + ec24String + ec1String;
                }

                stringreaderfour.Close();

                if (lengthCounterInt == 6)
                {
                    //then we move into groups of 4 binary digits
                    firECString = ec2String + ec3String + ec4String + ec5String;
                    secECString = ec6String + ec7String + ec8String + ec9String;
                    thiECString = ec10String + ec11String + ec12String + ec13String;
                    fouECString = ec14String + ec15String + ec16String + ec17String;
                    fifECString = ec18String + ec19String + ec20String + ec21String;
                    sixECString = ec22String + ec23String + ec24String + ec1String;
                }


                if (lengthCounterInt == 8)
                {
                    //then we move into groups of 4 binary digits
                    firECString = ec2String + ec3String + ec4String + ec5String;
                    secECString = ec6String + ec7String + ec8String + ec9String;
                    thiECString = ec10String + ec11String + ec12String + ec13String;
                    fouECString = ec14String + ec15String + ec16String + ec17String;
                    fifECString = ec18String + ec19String + ec20String + ec21String;
                    sixECString = ec22String + ec23String + ec24String + ec25String;
                    sevECString = ec26String + ec27String + ec28String + ec29String;
                    eigECString = ec30String + ec31String + ec32String + ec1String;
                }

                //then we convert the binary back to decimal
                //then we change the decimal according to the gg chart
                //A=0 P=2 Z=4 L=6 G=8 I=10 T=12 Y=14
                //E=1 O=3 X=5 U=7 K=9 S=11 V=13 N=15

                tempECString = firECString;
                EncipherBinaryBackToDecimal();
                firECString = EncipherBinaryBackToDecimal();

                tempECString = secECString;
                EncipherBinaryBackToDecimal();
                secECString = EncipherBinaryBackToDecimal();

                tempECString = thiECString;
                EncipherBinaryBackToDecimal();
                thiECString = EncipherBinaryBackToDecimal();

                tempECString = fouECString;
                EncipherBinaryBackToDecimal();
                fouECString = EncipherBinaryBackToDecimal();

                tempECString = fifECString;
                EncipherBinaryBackToDecimal();
                fifECString = EncipherBinaryBackToDecimal();

                tempECString = sixECString;
                EncipherBinaryBackToDecimal();
                sixECString = EncipherBinaryBackToDecimal();

                if (lengthCounterInt == 8)
                {
                    tempECString = sevECString;
                    EncipherBinaryBackToDecimal();
                    sevECString = EncipherBinaryBackToDecimal();

                    tempECString = eigECString;
                    EncipherBinaryBackToDecimal();
                    eigECString = EncipherBinaryBackToDecimal();
                }
                //Final step, output the gg code
                gameGenieCodeTextBox.Text = firECString + secECString + thiECString + fouECString +
                    fifECString + sixECString;

                if (lengthCounterInt == 8)
                {
                    gameGenieCodeTextBox.Text += sevECString + eigECString;
                }
            }
        }

        private string EncipherBinaryBackToDecimal()
        {
            string tempEBBDString = "0";

            try
            {
                if (tempECString == "0000")
                {
                    tempEBBDString = "A";
                }
                if (tempECString == "0001")
                {
                    tempEBBDString = "E";
                }
                if (tempECString == "0010")
                {
                    tempEBBDString = "P";
                }
                if (tempECString == "0011")
                {
                    tempEBBDString = "O";
                }
                if (tempECString == "0100")
                {
                    tempEBBDString = "Z";
                }
                if (tempECString == "0101")
                {
                    tempEBBDString = "X";
                }
                if (tempECString == "0110")
                {
                    tempEBBDString = "L";
                }
                if (tempECString == "0111")
                {
                    tempEBBDString = "U";
                }
                if (tempECString == "1000")
                {
                    tempEBBDString = "G";
                }
                if (tempECString == "1001")
                {
                    tempEBBDString = "K";
                }
                if (tempECString == "1010")
                {
                    tempEBBDString = "I";
                }
                if (tempECString == "1011")
                {
                    tempEBBDString = "S";
                }
                if (tempECString == "1100")
                {
                    tempEBBDString = "T";
                }
                if (tempECString == "1101")
                {
                    tempEBBDString = "V";
                }
                if (tempECString == "1110")
                {
                    tempEBBDString = "Y";
                }
                if (tempECString == "1111")
                {
                    tempEBBDString = "N";
                }
            }
            catch
            {
                MessageBox.Show("I crashed moving binary back to decimal.", "Error 11:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return tempEBBDString;
        }

        private string DecodeEncipherString()
        {
            string tempECRString = "0";

            try
            {

                if (tempECString == "0")
                {
                    tempECRString = "0000";
                }
                if (tempECString == "1")
                {
                    tempECRString = "0001";
                }
                if (tempECString == "2")
                {
                    tempECRString = "0010";
                }
                if (tempECString == "3")
                {
                    tempECRString = "0101";
                }
                if (tempECString == "4")
                {
                    tempECRString = "0100";
                }
                if (tempECString == "5")
                {
                    tempECRString = "0101";
                }
                if (tempECString == "6")
                {
                    tempECRString = "0110";
                }
                if (tempECString == "7")
                {
                    tempECRString = "0111";
                }
                if (tempECString == "8")
                {
                    tempECRString = "1000";
                }
                if (tempECString == "9")
                {
                    tempECRString = "1001";
                }
                if (tempECString == "a" || tempECString == "A")
                {
                    tempECRString = "1010";
                }
                if (tempECString == "b" || tempECString == "B")
                {
                    tempECRString = "1011";
                }
                if (tempECString == "c" || tempECString == "C")
                {
                    tempECRString = "1100";
                }
                if (tempECString == "d" || tempECString == "D")
                {
                    tempECRString = "1101";
                }
                if (tempECString == "e" || tempECString == "E")
                {
                    tempECRString = "1110";
                }
                if (tempECString == "f" || tempECString == "F")
                {
                    tempECRString = "1111";
                }
            }
            catch
            {
                MessageBox.Show("I crashed during decimal to binary conversion.", "Error 12:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return tempECRString;
        }

        private void nesGGEncDecForm_Load(object sender, EventArgs e)
        {
            gameGenieCode2TextBox.ShortcutsEnabled = false;
            addressTextBox.ShortcutsEnabled = false;
            replaceValueTextBox.ShortcutsEnabled = false;
            compareValueTextBox.ShortcutsEnabled = false;
        }

    }
}